package com.spring.service.example.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.bean.example.Book;
import com.spring.bean.page.Page;
import com.spring.bean.page.PageView;
import com.spring.dao.example.BookDao;
import com.spring.exception.ServiceException;
import com.spring.service.ServiceSupport;
import com.spring.service.example.BookService;
import com.spring.web.formbean.example.BookForm;

import com.spring.bean.example.Sort;
import com.spring.service.LuceneSupport;
import com.spring.dao.example.SortDao;




@Service
@Transactional
public class BookServiceImpl extends ServiceSupport implements BookService {

	@Autowired
	private BookDao bookDao;
	
	@Autowired
	private SortDao sortDao;
	
	@Override
	public PageView<Book> queryForPage(Page page) throws ServiceException {
		try {
			return bookDao.queryForPage(page);
		} catch (Exception e) {
			throw new ServiceException("queryForPage -- error " + e.getMessage());
		}
	}

	@Override
	@Transactional(readOnly=false, rollbackFor=Exception.class)
	public boolean add(BookForm formBean) throws ServiceException {
		try {
			Book book = formToEntity(Book.class, formBean);
			int res;
			res = bookDao.save(book);
			book.setId(res);
			Sort sort = sortDao.getById(book.getSort_id());
			if(sort == null){
				throw new ServiceException("sort is null");
			}
			book.setSort(sort);
			// 建立索引
			LuceneSupport.createIndex(book, 1);
			return true;
		} catch (Exception e) {
			throw new ServiceException(" add --- " + e.getMessage());
		}
	}
	
	@Override
	@Transactional(readOnly=false, rollbackFor=Exception.class)
	public boolean update(BookForm formBean) throws ServiceException {
		try {
			Boolean res = true;
			Book book = formToEntity(Book.class, formBean);
			res = bookDao.update(book);
			
			// 重建索引
			Integer sort_id = book.getSort_id();
			Sort sort = sortDao.getById(sort_id);
			if(sort == null){
				try {
					throw new Exception("");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			book.setSort(sort);
			LuceneSupport.rebuildIndexById(book, 1);
			return res;
		} catch (Exception e) {
			throw new ServiceException(e.getMessage());
		}
	}

	@Override
	public boolean delete(BookForm formBean)throws ServiceException {
		try {
			return bookDao.delete(formToEntity(Book.class, formBean));
		} catch (Exception e) {
			throw new ServiceException(e.getMessage());
		}
	}

	@Override
	public Book getById(Integer id) throws ServiceException {
		try {
			return bookDao.getById(id);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage());
		}
	}

	@Override
	public PageView<Book> searchForPage(String keyword, Page page) throws ServiceException {
		try {
			List<String> columns = new ArrayList<String>();
			columns.add("bookName");
			columns.add("profile");
			PageView<Book> pageView = null;
			pageView = LuceneSupport.searchForPage(Book.class, keyword, columns, page, 1);
			return pageView;
		} catch (Exception e) {
			throw new ServiceException(e.getMessage());
		}
	}

}
